<template>
  <div class="permission">
    <el-card>
      <div class="right">
        <el-button size="small" type="primary" @click="addEvent">添加权限</el-button>
      </div>
    </el-card>

    <el-table :data="list" border class="table" row-key="id">
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="code" label="标识" align="center" />
      <el-table-column prop="description" label="描述" align="center" />
      <el-table-column label="操作" width="300px" align="center">
        <template v-slot="{row}">
          <div>
            <el-button v-if="row.type===1" type="text" @click="addEvent2(row)">添加</el-button>
            <el-button type="text" @click="editEvent(row)">编辑</el-button>
            <el-button type="text" @click="delEvent(row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增弹框组件 -->
    <Add ref="add" @getData="getData" />
  </div>
</template>
<script>
import { sysPermission, sysPermissionDelete } from '@/api/permission'
import { changeData } from '@/utils/index'
import Add from './components/Add.vue'
export default {
  components: {
    Add
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async  getData() {
      const res = await sysPermission()
      this.list = changeData(res.data, '0')
      console.log('权限列表', this.list)
    },
    // 新增点击
    addEvent() {
      // 打开弹框
      this.$refs.add.show = true
      // 修改type与pid
      this.$refs.add.form.pid = '0' // 新增的最外层数据
      this.$refs.add.form.type = 1 // 新增页面
      this.$refs.add.mode = 'add'
    },
    // 新增点击2
    addEvent2(row) {
      // 打开弹框
      this.$refs.add.show = true
      this.$refs.add.form.pid = row.id
      this.$refs.add.form.type = 2 // 新增按钮
      this.$refs.add.mode = 'add'
    },
    // 编辑点击
    editEvent(row) {
      // 打开弹框
      this.$refs.add.show = true
      // 数据回显（深拷贝）
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    },
    // 删除点击事件
    async  delEvent(id) {
      await this.$confirm('您确定删除该项吗')
      await sysPermissionDelete(id)
      this.$message.success('删除成功')
      this.getData()
    }
  }
}
</script>
<style lang="scss" scoped>
.permission{
  padding:30px;
  .right{
    text-align: right;
  }
  .table{
    margin-top:15px;
  }
}
</style>
